<?php
namespace app\platform\controller\admin;
use app\BaseController;
use app\common\logic\DataSettingLogic;
use app\common\model\AppModel;
use app\common\model\SmsTplModel;
use app\common\logic\DataLogic;
class Setting extends BaseController{
    
    public function getData(){
        $key = $this->request->param('key');
        if(empty($key)){
            return $this->error('请选择要操作的配置');
        }
        $DataSettingLogic = new DataSettingLogic();
        $datas = $DataSettingLogic->getData($key);
        return $this->success([
            'datas' => $datas
        ]);
    }
    public function saveData(){
        $key = $this->request->param('key');
        if(empty($key)){
            return $this->error('请选择要操作的配置');
        }
        $datas = $this->request->param('datas','', 'security_json_or_array');
        $DataSettingLogic = new DataSettingLogic();
        $DataSettingLogic->saveData($key, $datas);
        return $this->success('保存成功');
    }   
    
    
    public function getSmsTplList()
    {

        $where=[
            ['is_delete','=',0],
        ];
        $sms_tpl_model= new SmsTplModel();
        $order=['add_time'=>'desc'];
        $append=['add_time_format'];
        $data=DataLogic::getDataList($sms_tpl_model,$where,$order,'*',$this->request->limit,$this->request->page,$append);
        return $this->success($data);
    }

    public function showSmsTpl()
    {
        $tpl_id=$this->request->param('tpl_id/d',0);
        if($tpl_id==0) return $this->success($return);
        if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');
        if($sms_tpl->is_delete == 1 ) return $this->error('模板不存在');
        $detail=[
            'sms_id'=>$sms_tpl->sms_id,
            'tpl'=>$sms_tpl->tpl,
            'key'=>$sms_tpl->key,
            'effect'=>$sms_tpl->effect,
        ];
        $return['detail']=$detail;
        return $this->success($return);
    }
    public function delSmsTpl(){
         $tpl_id=$this->request->param('tpl_id/d',0);
        if(empty($tpl_id)) return $this->error ('模版不存在');
        if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');
        $sms_tpl->is_delete = 1;
        $sms_tpl->save();
        return $this->success('删除成功');
    }

    public function saveSmsTpl()
    {
        

        $tpl_id=$this->request->param('tpl_id/d',0);
        $sms_id=$this->request->param('sms_id');
        $tpl=$this->request->param('tpl');
        $key=$this->request->param('key');
        $effect=$this->request->param('effect');
        if(empty($sms_id)) return $this->error('请输入模板ID');
        if(empty($tpl)) return $this->error('请输入模板内容');
        if(empty($key)) return $this->error('请输入发送的英文KEY');
        if(preg_match('/[^A-Za-z]+/', $key))  return $this->error('英文KEY请输入全字母');
        if(empty($effect)) return $this->error('请输入作用');

        if($tpl_id != 0) {
            if(null === $sms_tpl = SmsTplModel::find($tpl_id)) return $this->error('模板不存在');
            if($sms_tpl->is_delete == 1 ) return $this->error('模板不存在');
            //判断同一个app_id下key的唯一性
            if($sms_tpl->key != $key){
                if(null !== SmsTplModel::where([
                        ['key','=',$key],
                        ['is_delete','=',0]
                    ])->find()){
                    return $this->error('英文key在这个应用中已存在');
                }
            }
        }else{
            $sms_tpl = new SmsTplModel();
            if(null !== SmsTplModel::where([
                    ['key','=',$key],
                    ['is_delete','=',0]
                ])->find()){
                return $this->error('英文key在这个应用中已存在');
            }
        }
        $sms_tpl->sms_id = $sms_id;
        $sms_tpl->tpl=$tpl;
        $sms_tpl->key=$key;
        $sms_tpl->effect=$effect;
        $sms_tpl->save();

        return $this->success('保存成功');
    }

}